package org.wanxian.stores.db;

import java.util.ArrayList;

import org.wanxian.stores.model.Indent;
import org.wanxian.stores.util.CommonUtil;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class IndentDao {
	
	private DatabaseOpenHelper dbHelper;
	private static final String TABLE_NAME = "indent";
	
	public IndentDao(Context context) {
		dbHelper = new DatabaseOpenHelper(context);
	}
	
	public int insert(Indent indent) {
		Indent bef = queryById(indent.getId());
		if(bef != null) {
			deleteById(bef.getId());
		}
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("userId", indent.getUserId());
		values.put("storeId", indent.getStoreId());
		values.put("money", indent.getMoney());
		int id = (int) db.insert(TABLE_NAME, null, values);
		return id;
	}
	
	public Indent queryById(int id) {
		Indent indent = null;
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, "id = ?", new String[]{String.valueOf(id)}, null, null, null);
		while(cursor.moveToNext()) {
			int userId= cursor.getInt(cursor.getColumnIndex("userId"));
			int storeId = cursor.getInt(cursor.getColumnIndex("storeId"));
			int money = cursor.getInt(cursor.getColumnIndex("money"));
			
			indent = new Indent(id, userId, storeId, money);
		}
		cursor.close();
		db.close();
		return indent;
	}
	
	public void deleteById(int id) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.delete(TABLE_NAME, "id = ?", new String[] {String.valueOf(id)});
		db.close();
	}
	
	public ArrayList<Indent> queryAllByStoreId(int storeId) {
		ArrayList<Indent> list = new ArrayList<Indent>();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, "storeId = ?", new String[]{String.valueOf(storeId)}, null, null, null);
		while(cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			int userId = cursor.getInt(cursor.getColumnIndex("userId"));
			int money = cursor.getInt(cursor.getColumnIndex("money"));
			Indent indent = new Indent(id, userId, storeId, money);
			list.add(indent);
		}
		cursor.close();
		db.close();
		return list;
	}
	
	
	public ArrayList<Indent> queryAll() {
		ArrayList<Indent> list = new ArrayList<Indent>();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
		while(cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			int userId = cursor.getInt(cursor.getColumnIndex("userId"));
			int storeId = cursor.getInt(cursor.getColumnIndex("storeId"));
			int money = cursor.getInt(cursor.getColumnIndex("money"));
			Indent indent = new Indent(id, userId, storeId, money);
			list.add(indent);
		}
		cursor.close();
		db.close();
		return list;
	}
	
	public int getcount() {
		return queryAll().size();
	}
	
	
}
